﻿using System;
using System.Linq;
using EPiServer.Core;
using System.Collections.Generic;

namespace EPiSugar.Navigation
{
    public static class PageDataExtensions
    {
        public static PageData Parent(this PageData page)
        {
            return Current.PageSource.GetPage(page.ParentLink);
        }

        public static IEnumerable<PageData> Ancestors(this PageData page)
        {
            while(page.ParentLink.HasValue())
            {
                PageData parent = page.ParentLink.Page();
                yield return parent;
                page = parent;
            }
        }

        public static IEnumerable<PageData> Ancestors(this PageData page, PageReference stopBefore)
        {
            if (page.PageLink == stopBefore)
                yield break;

            foreach(PageData ancestor in page.Ancestors())
            {
                if (ancestor.PageLink == stopBefore)
                    yield break;
                yield return ancestor;
            }
        }

        public static IEnumerable<PageData> Children(this PageData page)
        {
            return page.PageLink.Children();
        }

        public static IEnumerable<PageData> Children(this PageData page, Func<PageData, bool> predicate)
        {
            return page.PageLink.Children().Where(predicate);
        }

        public static IEnumerable<PageData> Descendants(this PageData page)
        {
            return page.PageLink.Descendants();
        }

        public static IEnumerable<PageData> Descendants(this PageData page, Func<PageData, bool> predicate)
        {
            return page.PageLink.Descendants().Where(predicate);
        }

        public static IEnumerable<PageData> Descendants(this PageData page, int recursionDepth)
        {
            return page.PageLink.Descendants(recursionDepth);
        }

        public static IEnumerable<PageData> Descendants(this PageData page, int recursionDepth, Func<PageData, bool> predicate)
        {
            return page.PageLink.Descendants(recursionDepth).Where(predicate);
        }

        public static IEnumerable<PageData> Siblings(this PageData page)
        {
            foreach (PageData sibling in page.ParentLink.Children())
                yield return sibling;
        }

        public static IEnumerable<PageData> PrecedingSiblings(this PageData page)
        {
            foreach (PageData sibling in page.ParentLink.Children())
            {
                if(sibling == page)
                    break;
                yield return sibling;
            }
        }

        public static IEnumerable<PageData> SubsequentSiblings(this PageData page)
        {
            bool found = false;
            foreach (PageData sibling in page.ParentLink.Children())
            {
                if(sibling == page)
                    found = true;
                else if (found)
                    yield return sibling;
            }
        }

        public static EnumerationAppender Add(this PageData page)
        {
            return new EnumerationAppender(new[] {page});
        }
    }
}